/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.Main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *
 * @author Kumori
 */
public class ProcessFile {/*Read and write file demo. There's nothing to do with FP.*/

    private void readFile(File file) {
         try {
	    // Open file
           // String nameOfFile ="test.txt";
           // String path = "D:\\My Books\\My Document\\Test Data";
          //  File file1 = new File(path,nameOfFile);
            FileReader fReader = new FileReader(file);
	        fileInput = new BufferedReader(fReader);
	        String line = fileInput.readLine();
	        int maxNum =0; 
	        while (line!=null){
	            StringTokenizer dataLine = new StringTokenizer(line);
	            while (dataLine.hasMoreElements()){
	               int nextNum = Integer.parseInt(dataLine.nextToken());
	               if (nextNum > maxNum) maxNum = nextNum; 
                }
	            line = fileInput.readLine();
	        }
	        System.out.println("So lon nhat la:"+maxNum);
	    }
	catch(IOException ioException) {
	    System.out.println("Error opening file");
	    System.exit(1);
	    }
        
    }
    protected BufferedReader fileInput;
    private String[] testArray;//= new String[23];
    protected String cleanedString;
    private String testClassField;
    public String getTestClassField() {
		return testClassField;
	}

	public void setTestClassField(String testClassField) {
		this.testClassField = testClassField;
	}

	public static void main(String args[]){
		String s1="Hello";
		String s2=new String("Hello");
		System.out.println(s1 + " equals " + s2 + " -> " + 
		s1.equals(s2)); 
		System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2)); 
                      
	}
    
    private int putParam(int d) {
		// TODO Auto-generated method stub
		d=3;
		return d;
	}

	protected void removeDup() throws IOException {
        String line = fileInput.readLine();
        int i=0;
        setTestArray(new String[23]);
        try{
        String path = "D:\\My Books\\My Document\\Test Data";   
        String nameOfFile ="mushroom.txt";
        File file1 = new File(path,nameOfFile);
        FileWriter fstream = new FileWriter(file1);
        BufferedWriter out = new BufferedWriter(fstream);
	while (line != null) {
	    //checkLine(counter+1,line);
	    StringTokenizer dataLine = new StringTokenizer(line);
            int numberOfTokens = dataLine.countTokens();
	    if (numberOfTokens == 0) break;
            i=0;
            while (dataLine.hasMoreElements()){
                    getTestArray()[i] = dataLine.nextToken(",");
               i++;
            }
            cleanedString = checkDup(getTestArray()) +"\n";
            out.write(cleanedString);
	    line = fileInput.readLine();
	    }
        out.close();
        }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
      }
        
      }
      
      public void writeFile(File file){
      try{
  // Create file 
        //String path = "D:\\My Books\\My Document\\Test Data";   
        //String nameOfFile ="weightForT10I4D100K";
        //file = new File(path,nameOfFile);
        FileWriter fstream = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fstream);
         out.write("Hello Java");
  //Close the output stream
         out.close();
        }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
        }};
        
        public void writeAppend(File file,String strAppend){
            try{
        // Create file 
              //String path = "D:\\My Books\\My Document\\Test Data";   
              //String nameOfFile ="weightForT10I4D100K";
              //file = new File(path,nameOfFile);
              FileWriter fstream = new FileWriter(file,true);
              BufferedWriter out = new BufferedWriter(fstream);
               out.write(strAppend+"\n");
        //Close the output stream
               out.close();
              }catch (Exception e){//Catch exception if any
               System.err.println("Error: " + e.getMessage());
              }};
      protected String checkDup(String[] testArray){
          String tempString = "";
          String charToCheck= "";
          int index =0;
          for (int i=0;i<testArray.length;i++){
              charToCheck = testArray[i];
              if (!tempString.contains(charToCheck)){
              tempString =tempString + charToCheck +",";
              }
              
          }
         return tempString.substring(0,tempString.length()-1);
      };

    private void rearange()throws IOException {
        String line = fileInput.readLine();
        int i=0;
        setTestArray(new String[5]);
        try{
        String path = "D:\\My Books\\My Document\\Test Data";   
        String nameOfFile ="test1.txt";
        File file1 = new File(path,nameOfFile);
        FileWriter fstream = new FileWriter(file1);
        BufferedWriter out = new BufferedWriter(fstream);
	while (line != null) {
	    //checkLine(counter+1,line);
	    StringTokenizer dataLine = new StringTokenizer(line);
            int numberOfTokens = dataLine.countTokens();
	    if (numberOfTokens == 0) break;
            i=0;
            while (dataLine.hasMoreElements()){
                    getTestArray()[i] = dataLine.nextToken();
               i++;
            }
            cleanedString = rearrange(getTestArray()) +"\n";
            out.write(cleanedString);
	    line = fileInput.readLine();
	    }
        out.close();
        }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
      }
       
    }

    private String rearrange(String[] testArray) {
        String result="";
        String tmp = "";
        boolean swapped = true;
        int j = 0;
        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < testArray.length - j; i++) {
                
                if (Integer.parseInt(testArray[i]) > Integer.parseInt(testArray[i + 1])) {
                    tmp = testArray[i];
                    testArray[i] = testArray[i + 1];
                    testArray[i + 1] = tmp;
                    swapped = true;
                }
            }
        }
        for (String str: testArray){
           result += str +" ";
        }
        return result;
    }

    private String bubleSort(int[] testArray) {
         String result="";
        int tmp = 0;
        int counter=0;
        for (int i=0;i<testArray.length;i++){
            for (int j = i+1; j < testArray.length; j++) {
                counter++;
                if (testArray[i] > testArray[j]) {
                    tmp = testArray[i];
                    testArray[i] = testArray[j];
                    testArray[j] = tmp;
                    //swapped = true;
                }
            }
        }
      /*  boolean swapped = true;
        int j = 0;
        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < testArray.length - j; i++) {
                counter++;
                if (testArray[i] > testArray[i+1]) {
                    tmp = testArray[i];
                    testArray[i] = testArray[i + 1];
                    testArray[i + 1] = tmp;
                    swapped = true;
                }
            }
        }*/
        for (int st:testArray)
        System.out.println(st+" ");
        System.out.print("Counter: "+counter);
        return result;
    }

    /**
     * @return the testArray
     */
    public String[] getTestArray() {
        return testArray;
    }

    /**
     * @param testArray the testArray to set
     */
    public void setTestArray(String[] testArray) {
        this.testArray = testArray;
    }
    }

